package me.zd.tool.hardlink.view.listener;

import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.function.Consumer;

public class ComboBoxChangedListener<T> implements ItemListener {
    private final Consumer<T> changedOperation;
    private final ModelLock modelLock;

    public ComboBoxChangedListener(JComboBox<T> component, Consumer<T> changedOperation, ModelLock modelLock) {
        this.changedOperation = changedOperation;
        this.modelLock = modelLock;
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        if (!modelLock.isLocked() && e.getStateChange() == ItemEvent.SELECTED) {
            //noinspection unchecked
            changedOperation.accept((T) e.getItem());
        }
    }
}
